﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LyData.Data;

public partial class Admin_CodeAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.AddTime.Text = DateTime.Now.ToString();
        }
    }


    protected void Add_Click(object sender, EventArgs e)
    {
        string path = string.Format("{0}{1}/{2}", FSO.GetUploadPath(), "Ad", FSO.RandomFileName(this.PicSmall.FileName));//

        string filePath = base.Server.MapPath(path);//生成的图片
        FSO.PathExists(filePath);//查找目录，没有就创建
        this.PicSmall.SaveAs(filePath);

        string[,] includeColumns = new string[,] {
                        { "imgpath", path, "NText", "0" }, 
                        { "codeClassid", this.ArticleClassIdList.SelectedValue.ToString(), "int", "0" }, 
                        { "Title", this.txtTitle.Text, "NVarChar", "0" },                         
                        { "AddTime", this.AddTime.Text, "DateTime", "0" },                         
                        { "Curl",txturl.Text,"NText","0" },
                        { "Weight", this.txtorder.Text,"int","32" },
                        { "cwidth", this.txtwidth.Text,"int","32" },
                        { "cheight", this.txtheight.Text,"int","32" }
                    };

        IDb db = new DbFactory().DriverDb();
        db.Open();
        db.Insert("Codelist", includeColumns);
        db.Close();
        this.Msg.Text = "图片添加成功！";
        this.Msg.Visible = true;
        this.txtTitle.Text = "";
        this.txturl.Text = "";
        this.AddTime.Text = DateTime.Now.ToString();

    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            Convert.ToDateTime(this.AddTime.Text);
            args.IsValid = true;
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void PicSmallCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (this.PicSmall.HasFile)
        {
            string extension = FSO.GetExtension(this.PicSmall.FileName);
            string extensionList = ".jpg|.gif|.png|.bmp";
            args.IsValid = FSO.ExtensionValid(extension, extensionList);
            this.PicSmallCustomValidator.ErrorMessage = "小图格式错误，合法格式：" + extensionList;
        }
        else
        {
            this.PicSmallCustomValidator.ErrorMessage = "小图路径选择错误！";
            args.IsValid = false;
        }
    }
}
